<?php

class Nuo_Html_Element_Multigroup extends Nuo_Html_Element_Group
{
	/**
	 * @var bool
	 */
	protected $_isMulti = TRUE;

	/**
	 * @var bool
	 */
	protected $_toMulti = FALSE;

	/**
	 * @var int
	 */
	protected $_minNumber = 1;

	/**
	 * @var int
	 */
	protected $_maxNumber = 5;

	/**
	 * @var bool
	 */
	protected $_prepared = FALSE;

	/**
	 * @var array
	 */
	protected $_meta = array(
		'tag' => 'div',
		'render' => 'div2',
	);
	
	/**
	 * overwrite set name 
	 */
	public function setName($name)
	{
		$name .= '[0]';
		$this -> _name = $name;
		return $this;

	}

	/**
	 * @param int $number
	 * @return Nuo_Html_Element_Multigroup
	 */
	public function setMinNumber($number)
	{
		$this -> _minNumber = (int)$number;
		return $this;
	}

	/**
	 * @return int
	 */
	public function getMinNumber()
	{
		return $this -> _minNumber;
	}

	/**
	 * @param int $number
	 * @return Nuo_Html_Element_Multigroup
	 */
	public function setMaxNumber($number)
	{
		$this -> _maxNumber = (int)$number;
		return $this;
	}

	/**
	 * @return int
	 */
	public function getMaxNumber()
	{
		return $this -> _maxNumber;
	}

	/**
	 * @return array
	 */
	public function getValue()
	{
		$data = array();

		foreach ($this->_children as $index => $children)
		{
			foreach ($children as $name => $element)
			{
				$data[$index][$name] = $element -> getValue();
			}

		}
		return $data;
	}

	/**
	 * set value
	 * @param array $data
	 * @return Nuo_Html_Element
	 */
	public function setValue($data)
	{
		if (is_array($data))
		{
			$number = min(max(count($data), $this -> _minNumber), $this -> _maxNumber);
			$this -> _prepareElement($number);
		}
		else
		{
			return;
		}

		foreach ($this->_children as $children)
		{
			$data2 = array_shift($data);
			
			foreach ($children as $name => $element)
			{
				$element -> setValue(isset($data2[$name]) ? $data2[$name] : null);
			}
		}
		return $this;
	}

	/**
	 * prepare element
	 */
	public function _prepareElement($number)
	{
		if ($this -> _prepared)
		{
			return;
		}

		$children = $this -> _children;

		$this -> _children = array();

		$number = (int)$number;

		for ($i = 0; $i < $number; ++$i)
		{
			$k = get_random_text(4);

			foreach ($children as $name => $element)
			{
				$this -> _children[$i][$name] = $newEle = clone $element;

				$newName = $newEle -> getName();

				$newName = str_replace('[0]', "[{$k}]", $newName);

				$newEle -> setName($newName);
			}
		}

		$this -> _prepared = TRUE;
	}
	
	/**
	 * @see Nuo_Html_Element::render
	 * @return Nuo_Html_Element_MultiGroup
	 */
	public function beforeRender()
	{
		parent::beforeRender();
		$this -> _prepareElement($this -> _minNumber);
		return $this;
	}

	/**
	 * @param array $data
	 * @return bool
	 */
	public function isValid($data)
	{
		if (is_array($data))
		{
			$number = min(max(count($data), $this -> _minNumber), $this -> _maxNumber);
		}
		else
		{
			$number = $this -> _minNumber;
		}

		$this -> _prepareElement($number);

		$flag = true;

		if (is_array($data))
		{
			foreach ($this->_children as $children)
			{
				$data2 = array_shift($data);

				foreach ($children as $name => $ele)
				{
					if (!$ele -> isValid(isset($data2[$name]) ? $data2[$name] : NULL))
					{
						$flag = false;
					}
				}
			}
		}

		return $flag;
	}

}
